package com.onurkaganaldemir.ktoastlib;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * Created by mimcrea on 21.02.2018.
 *
 * @since 2018-02-21
 */
public class KToast {
    /**
     * LENGTH_LONG
     */
    public static final int LENGTH_LONG = 4000;
    /**
     * LENGTH_SHORT
     */
    public static final int LENGTH_SHORT = 2000;
    /**
     * LENGTH_AUTO
     */
    public static final int LENGTH_AUTO = 1000;

    /**
     * Error type toast.
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     */
    public static void errorToast(final Context context, String message, final int gravity, int duration) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_error_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtErrorToast)).setText(message);

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity);
            }
        });
        countDownTimer.start();
    }

    /**
     * Warning type toast.
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     */
    public static void warningToast(final Context context, String message, final int gravity, int duration) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_warning_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtWarningToast)).setText(message);

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity);
            }
        });
        countDownTimer.start();
    }

    /**
     * Info type toast.
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     */
    public static void infoToast(final Context context, String message, final int gravity, int duration) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_info_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtInfoToast)).setText(message);

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity);
            }
        });
        countDownTimer.start();
    }

    /**
     * Success type toast.
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     */
    public static void successToast(final Context context, String message, final int gravity, int duration) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_success_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtSuccessToast)).setText(message);

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity);
            }
        });
        countDownTimer.start();
    }

    /**
     * Normal type toast.
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     */
    public static void normalToast(final Context context, String message, final int gravity, int duration) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_normal_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtNormalToast)).setText(message);

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity);
            }
        });
        countDownTimer.start();
    }

    /**
     * Normal type toast with icon.
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     * @param icon icon
     */
    public static void normalToast(final Context context, String message, final int gravity, int duration, int icon) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_normal_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtNormalToast)).setText(message);
        view.findComponentById(ResourceTable.Id_normalToastImg).setVisibility(Component.VISIBLE);
        ((Image) view.findComponentById(ResourceTable.Id_normalToastImg)).setImageElement(new VectorElement(context, icon));

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity);
            }
        });
        countDownTimer.start();
    }

    /**
     * Custom toast background color.
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     * @param toastColor toastColor
     */
    public static void customColorToast(final Context context, String message, final int gravity, int duration, int toastColor) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_custom_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtCustomToast)).setText(message);

        ShapeElement element = new ShapeElement(context, ResourceTable.Graphic_background_custom_toast);
        setColorTint(context, element, toastColor);
        view.findComponentById(ResourceTable.Id_customToastLyt).setBackground(element);

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity);
            }
        });
        countDownTimer.start();
    }

    /**
     * Custom toast background color and icon.
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     * @param toastColor toastColor
     * @param icon icon
     */
    public static void customColorToast(final Context context, String message, final int gravity, int duration, int toastColor, int icon) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_custom_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtCustomToast)).setText(message);

        ShapeElement element = new ShapeElement(context, ResourceTable.Graphic_background_custom_toast);
        setColorTint(context, element, toastColor);
        view.findComponentById(ResourceTable.Id_customToastLyt).setBackground(element);

        view.findComponentById(ResourceTable.Id_customToastImg).setVisibility(Component.VISIBLE);
        ((Image) view.findComponentById(ResourceTable.Id_customToastImg)).setImageElement(new VectorElement(context, icon));

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity);
            }
        });
        countDownTimer.start();
    }

    /**
     * customBackgroudToast
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     * @param toastBackground toastBackground
     * @param textColor textColor
     */
    public static void customBackgroudToast(final Context context, String message, final int gravity, int duration, int toastBackground, Integer textColor) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_custom_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtCustomToast)).setText(message);

        if (textColor != null) {
            ((Text) view.findComponentById(ResourceTable.Id_txtCustomToast)).setTextColor(new Color(textColor));
        }

        view.findComponentById(ResourceTable.Id_customToastLyt).setBackground(new ShapeElement(context, toastBackground));

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity, 25);
            }
        });
        countDownTimer.start();
    }

    /**
     * customBackgroudToast
     *
     * @param context context
     * @param message message
     * @param gravity gravity
     * @param duration duration
     * @param toastBackground toastBackground
     * @param textColor textColor
     * @param icon icon
     */
    public static void customBackgroudToast(final Context context, String message, final int gravity, int duration, int toastBackground, Integer textColor, int icon) {
        final Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_custom_toast, null, true);

        ((Text) view.findComponentById(ResourceTable.Id_txtCustomToast)).setText(message);

        if (textColor != null) {
            ((Text) view.findComponentById(ResourceTable.Id_txtCustomToast)).setTextColor(new Color(textColor));
        }

        view.findComponentById(ResourceTable.Id_customToastLyt).setBackground(new ShapeElement(context, toastBackground));

        view.findComponentById(ResourceTable.Id_customToastImg).setVisibility(Component.VISIBLE);
        ((Image) view.findComponentById(ResourceTable.Id_customToastImg)).setImageElement(new VectorElement(context, icon));

        int tempDuration = duration;
        if (duration == LENGTH_AUTO) {
            tempDuration = Util.toastTime(message);
        }

        CountDownTimer countDownTimer = new CountDownTimer(Math.max(tempDuration + 1000, 1000), 2000);
        countDownTimer.setCountDownListener(new CountDownTimer.TimerListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                showToastDialog(context, view, gravity, 25);
            }
        });
        countDownTimer.start();
    }

    private static void setColorTint(Context context, Element element, int toastColor) {
        try {
            ElementTintUtil.setColorTint(element, context.getResourceManager().getElement(toastColor).getColor());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
    }

    private static void showToastDialog(Context context, Component view, int gravity) {
        showToastDialog(context, view, gravity, Constants.CORNER_RADIUS);
    }

    private static void showToastDialog(Context context, Component view, int gravity, int radius) {
        ToastDialog toast = new ToastDialog(context);
        toast.setComponent(view);
        toast.setDuration(LENGTH_SHORT);
        if (gravity == LayoutAlignment.BOTTOM) {
            toast.setOffset(0, AttrHelper.vp2px(Constants.TOAST_OFFSET_X, context));
        }
        toast.setCornerRadius(AttrHelper.vp2px(radius, context));
        toast.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toast.setAlignment(gravity);
        toast.show();
    }
}
